﻿//using AutoMapper;
//using MediatR;
//using Microsoft.IdentityModel.Tokens;
//using RedDragon.Common.Domain.AuthorizeRBAC;
//using RedDragon.Common.Dtos;
//using RedDragon.Common.Infrastructure.Interface;
//using RedDragon.RBAC.ErrorCodeEnum;
//using T.Common.Domain.Common;
//using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

//namespace RedDragon.RBAC.API.Application.Common.Resourec
//{
//    public class ShowResoureHandler : IRequestHandler<ShowResourCommand, Result>
//    {
//        /// <summary>
//        /// mapper
//        /// </summary>
//        private readonly IMapper mapper;
//        /// <summary>
//        /// 权限仓储
//        /// </summary>
//        public readonly IBaseRepository<Resource> resourceRepository;


//        public ShowResoureHandler(IMapper mapper, IBaseRepository<Resource> resourceRepository)
//        {
//            this.mapper = mapper;
//            this.resourceRepository = resourceRepository;
//        }
//        /// <summary>
//        /// 资源显示
//        /// </summary>
//        /// <param name="request"></param>
//        /// <param name="cancellationToken"></param>
//        /// <returns></returns>
//        public async Task<Result> Handle(ShowResourCommand request, CancellationToken cancellationToken)
//        {
//            var result = new Result();
//            var list = await (await resourceRepository.GetAll()).ToArrayAsync();

//           

//            result.Data = list;
//            return result;
//        }
//    }
//}
using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Dtos;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.ErrorCodeEnum;
using T.Common.Domain.Common;
using System.Collections.Generic; // 需要引用
using System.Linq; // 需要引用
using System.Threading; // 需要引用
using System.Threading.Tasks;
using RedDragon.RBAC.API.Application.Common.Resourec; // 需要引用
using Microsoft.IdentityModel.Tokens;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace RedDragon.RBAC.API.Application.Common.Resourec
{
    public class ShowResourceHandler : IRequestHandler<ShowResourCommand, Result>
    {
        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 资源仓储
        /// </summary>
        private readonly IBaseRepository<Resource> resourceRepository;

        public ShowResourceHandler(IMapper mapper, IBaseRepository<Resource> resourceRepository)
        {
            this.mapper = mapper;
            this.resourceRepository = resourceRepository;
        }

        /// <summary>
        /// 资源显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>


        public async Task<Result> Handle(ShowResourCommand request, CancellationToken cancellationToken)
        {

            // 获取所有资源
            var list = await (await resourceRepository.GetAll()).ToArrayAsync();

            var result = new Result();

            // 如果请求中包含资源名，则过滤资源
            if (!string.IsNullOrEmpty(request.ResourceName))
            {
                list = list.Where(x => x.ResourceName == request.ResourceName).ToArray();
            }
            if (!string.IsNullOrEmpty(request.ResourceNo))
            {
                list = list.Where(x => x.ResourceNo == request.ResourceNo).ToArray();
            }
            if (!string.IsNullOrEmpty(request.ResourceType))
            {
                list = list.Where(x => x.ResourceType == request.ResourceType).ToArray();
            }
            if (!string.IsNullOrEmpty(request.ResourceSmark))
            {
                list = list.Where(x => x.ResourceSmark == request.ResourceSmark).ToArray();
            }

            result.Data = list;
            return result;
        }
    }
}

